﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Http;
using Jw.Models;
using Jw.Core;
using Jw.Web.Areas.Senv.Common;
using Newtonsoft.Json.Linq;
using Jw.Web.App_Start;

namespace Jw.Web.Areas.Senv.Controllers
{
    //安全检查记录
    public class AqjcjlController : BaseController
    {
        //安全检查记录
        // GET: /Senv/Aqjcjl/

        public ActionResult Index()
        {
            var codeService = new sys_codeService();
            var model = new
            {
                idField = "Aqjcjl_BillNo", //主键
                urls = SenvHelper.GetIndexUrls("Aqjcjl"),
                resx = SenvHelper.GetIndexResx("安全检查记录"),
                dataSource = new
                {
                    yearItems = codeService.GetYearItems(),
                    monthItems = codeService.GetMonthItems()
                },
                form = new
                {
                    Aqjcjl_BillNo = "", //单据编号
                    Aqjcjl_Sjbmbh = "",//受检单位
                    Aqjcjl_Jcbmbh = "",//检查单位
                    Aqjcjl_Jcrq = DateTime.Now.ToString("yyyy-MM-dd") //检查日期
                }
            };
            return View(model);
        }

        /// <summary>
        /// 安全检查记录明细
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [MvcMenuFilter(false)]
        public ActionResult Edit(string id = "")
        {
            var data = new AqjcjlApiController().GetEditMaster(id);
            var codeService = new sys_codeService();

            var model = new
            {
                form = data.form,
                scrollKeys = data.scrollKeys,
                urls = SenvHelper.GetEditUrls("Aqjcjl"),
                resx = SenvHelper.GetEditResx("安全查询记录"),
                dataSource = new
                {
                },
                defaultForm = new s_Aqjcjl().Extend(new
                {
                    Aqjcjl_BillNo = id
                }),
                defaultRow = new
                {
                },
                setting = new
                {
                    postFormKeys = new string[] { "Aqjcjl_BillNo" },
                }
            };

            return View(model);
        }
    }


    public class AqjcjlApiController : ApiController
    {
        public string GetNewBillNo()
        {
            return new s_AqjcjlService().GetNewKey("Aqjcjl_BillNo", "dateplus");
        }

        public dynamic Get(RequestWrapper query)
        {
            query.LoadSettingXmlString(@"
            <settings defaultOrderBy='Aqjcjl_BillNo DESC'>
            <select>*</select>
                <from>s_Aqjcjl</from>
                    <where defaultForAll='true' defaultCp='equal' defaultIgnoreEmpty='true' >
                        <field name='Aqjcjl_BillNo'     cp='like'></field>
                        <field name='CreateDate' cp='daterange'></field>
                    </where>
            </settings>");
            var service = new s_AqjcjlService();
            var pQuery = query.ToParamQuery();
            var result = service.GetDynamicListWithPaging(pQuery);

            return result;
        }

        public dynamic GetEditMaster(string id)
        {
            s_Aqjcjl obj = new s_AqjcjlService().GetModel(ParamQuery.Instance().AndWhere("Aqjcjl_BillNo", id));
            return new
            {
                form = obj,
                scrollKeys = new s_AqjcjlService().ScrollKeys("Aqjcjl_BillNo", id.ToString())
            };
        }

        public void Edit(dynamic data)
        {
            var formWrapper = RequestWrapper.Instance().LoadSettingXmlString(@"
            <settings>
                <table>{0}</table>
                <where><field name='Aqjcjl_BillNo' cp='equal'></field></where>
            </settings>", typeof(s_Aqjcjl).Name);



            var result = new s_AqjcjlService().Edit(formWrapper, null, data);
        }


        /// <summary>
        /// 删除主表（删除主表前是删除明细)
        /// </summary>
        /// <param name="id"></param>
        public void Delete(string id)
        {
            var result = new s_AqjcjlService().Delete(ParamDelete.Instance().AndWhere("Aqjcjl_BillNo", id));
            SenvHelper.ThrowHttpExceptionWhen(result <= 0, "单据删除失败[Aqjcjl_BillNo={0}]，请重试或联系管理员！", id);
        }

        //审核
        [System.Web.Http.HttpPost]
        public void Audit(string id, JObject data)
        {
            var status = data["status"].ToString();
            var comment = data["comment"].ToString();
            var result = new SenvService().Audit(typeof(s_Aqjcjl).Name, "Aqjcjl_BillNo", id, status, comment);
            SenvHelper.ThrowHttpExceptionWhen(result < 0, "单据审核失败[Aqjcjl_BillNo={0}]，请重试或联系管理员！", id);
        }
    }

}
